part of base.temples;

Widget buildRefreshWidget(
    {required Widget Function() builder,
    VoidCallback? onRefresh,
    VoidCallback? onLoad,
    required RefreshController refreshController,
    bool enablePullUp = true,
    bool enablePullDown = true}) {
  return SmartRefresher(
    enablePullUp: enablePullUp,
    enablePullDown: enablePullDown,
    controller: refreshController,
    onRefresh: onRefresh,
    onLoading: onLoad,
    header: const ClassicHeader(
      idleText: "下拉刷新",
      releaseText: "松开刷新",
      completeText: "刷新完成",
      refreshingText: "加载中......",
    ),
    footer: const ClassicFooter(
      idleText: "上拉加载更多",
      canLoadingText: "松开加载更多",
      loadingText: "加载中......",
    ),
    child: builder(),
  );
}

Widget buildRefreshListWidget<T, C extends PagingController<T, PagingState<T>>>(
    {required Widget Function(T item, int index) itemBuilder,
    bool enablePullUp = true,
    bool enablePullDown = true,
    String? tag,
    Widget Function(T item, int index)? separatorBuilder,
    Function(T item, int index)? onItemClick,
    ScrollPhysics? physics,
    Widget? errorWidget,
    Widget? emptyWidget,
    Widget? loadWidget,
    bool shrinkWrap = false,
    Axis scrollDirection = Axis.vertical}) {
  C controller = Get.find<C>(tag: tag);
  return GetBuilder<C>(
    builder: (controller) {
      return EmptyWidget(
        state: controller.emptyState,
        successWidget: buildRefreshWidget(
          builder: () => buildListView<T>(
              data: controller.pagingState.data,
              separatorBuilder: separatorBuilder,
              itemBuilder: itemBuilder,
              onItemClick: onItemClick,
              physics: physics,
              shrinkWrap: shrinkWrap,
              scrollDirection: scrollDirection),
          refreshController: controller.pagingState.refreshController,
          onRefresh: () => controller.refreshData(),
          onLoad: () => controller.loadMoreData(),
          enablePullDown: enablePullDown,
          enablePullUp: enablePullUp && controller.pagingState.enableLoadMore,
        ),
        errorWidget: errorWidget,
        emptyWidget: emptyWidget,
        loadWidget: loadWidget,
        errorRetry: () {
          controller.refreshData();
        },
      );
    },
    tag: tag,
    id: controller.pagingState.refreshId,
  );
}

Widget buildListView<T>(
    {required Widget Function(T item, int index) itemBuilder,
    required List<T> data,
    Widget Function(T item, int index)? separatorBuilder,
    Function(T item, int index)? onItemClick,
    ScrollPhysics? physics,
    padding = EdgeInsets.zero,
    bool shrinkWrap = false,
    Axis scrollDirection = Axis.vertical}) {
  return ListView.separated(
      shrinkWrap: shrinkWrap,
      physics: physics,
      padding: padding,
      scrollDirection: scrollDirection,
      itemBuilder: (ctx, index) => GestureDetector(
            child: itemBuilder.call(data[index], index),
            onTap: () => onItemClick?.call(data[index], index),
          ),
      separatorBuilder: (ctx, index) =>
          separatorBuilder?.call(data[index], index) ?? Container(),
      itemCount: data.length);
}

Widget
    buildNormalRefreshWidget<T, C extends PagingController<T, PagingState<T>>>(
        Widget Function() builder, C controller,
        {bool enablePullUp = true,
        bool enablePullDown = true,
        String? tag,
        ScrollPhysics? physics,
        Widget? errorWidget,
        Widget? emptyWidget,
        Widget? loadWidget,
        bool shrinkWrap = false,
        Axis scrollDirection = Axis.vertical}) {
  return GetBuilder<C>(
    builder: (controller) {
      return EmptyWidget(
        state: controller.emptyState,
        successWidget: buildRefreshWidget(
          builder: builder,
          refreshController: controller.pagingState.refreshController,
          onRefresh: () => controller.refreshData(),
          onLoad: () => controller.loadMoreData(),
          enablePullDown: enablePullDown,
          enablePullUp: enablePullUp && controller.pagingState.enableLoadMore,
        ),
        errorWidget: errorWidget,
        emptyWidget: emptyWidget,
        loadWidget: loadWidget,
        errorRetry: () {
          controller.refreshData();
        },
      );
    },
    tag: tag,
    id: controller.pagingState.refreshId,
  );
}
